"""
DB数据的参数化
"""

# 导包
import requests
import unittest
from parameterized import parameterized
from lib.dbutil import DBUtil
from api.login import LoginAPI

# 构建参数化数据
def build_data():
    test_data = []
    # 获取数据库数据
    sql = "select * from t_login"
    db_data = DBUtil.exe_sql(sql)
    for case_data in db_data:
            username = case_data[2]   # 此处的23456是数据库中username，password等等所对应的下标，解释图片我已发给手机
            password = case_data[3]
            verify_code = case_data[4]
            status_code = case_data[5]
            content_type = case_data[6]
            status = case_data[7]
            msg = case_data[8]
            test_data.append((username, password, verify_code, status_code, status, content_type, msg))  # 记住此处是一个双括号，因为内侧括号代表元组
            print(test_data)
    return test_data

# 创建测试类
class TestLogin1(unittest.TestCase):
    # 前置处理
    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    # 后置处理
    def tearDown(self):
        if self.session:
            self.session.close()

    # 创建测试用例
    @parameterized.expand(build_data())
    def test01_login_parameterized(self, username, password, verify_code, status_code, status, content_type, msg):
        # 调用验证码接口并断言
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code, response.status_code)
        self.assertIn(content_type, response.headers.get("Content-Type"))

        # 调用登陆接口并断言
        response = self.login_api.login(self.session, username, password, verify_code)
        print(response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get("status"))
        self.assertIn(msg, response.json().get("msg"))
